// pages/details/details.js
var {
    getGoodsDetail
} = require("../../api/Home.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgs: [],
        goods: {
            // id:2,
            // goodsImage:"/images/lists01.webp",
            // goodsName:"2商品名称",
            // goodsImgs:[
            //     "/images/banner01.jpg",
            //     "/images/banner02.jpg",
            //     "/images/banner03.jpg",
            //     "/images/banner04.jpg",
            // ],
            // goodsAddress:"广州",
            // goodsPrice:"200",
            // goodsPriceOld:"300",
            // goodsDetails:"/images/IMG_0466.JPG"
        },
        num: 0, //购物车商品数量
        collect: false, //判断商品是否收藏
    },
    goCart() {
        wx.switchTab({
            url: '/pages/car/car',
        })
    },
    goHome() {
        wx.switchTab({
            url: '/pages/home/home'
        })
    },
    // 加入购物车
    addShopping() {
        let cartLists = {
            id: this.data.goods.id,
            goodsImage: this.data.goods.goodsImage,
            goodsName: this.data.goods.goodsName,
            goodsPrice: this.data.goods.goodsPrice,
            num: 1,
        };
        let goodsCarList = wx.getStorageSync('goodsCarList');
        if (goodsCarList) {
            let index = goodsCarList.findIndex(item => item.id == this.data.goods.id);
            if (index != -1) {
                goodsCarList[index].num += 1;
            } else {
                goodsCarList.push(cartLists);
            }
            wx.setStorageSync('goodsCarList', goodsCarList);
        } else {
            wx.setStorageSync('goodsCarList', [cartLists])
        }
        this.setData({
            num: wx.getStorageSync('goodsCarList').length
        })
    },
    // 前往支付
    goPay() {
        wx.switchTab({
            url: '/pages/car/car',
        })
        this.addShopping();
    },
    // 点击收藏与取消收藏
    whetherNeed() {
        let id = this.data.goods.id;
        let collectLists = wx.getStorageSync('collectLists');
        if (this.data.collect) {
            this.setData({
                collect: false
            })
            let index = collectLists.findIndex(item=>item.id==id)
            collectLists.splice(index, 1);
            wx.setStorageSync('collectLists', collectLists);
            wx.showToast({
                title: "取消收藏成功",
                duration:2000
              })
        } else {
            this.setData({
                collect: true
            })
    
            if (collectLists) {
                let index = collectLists.findIndex(item=>item.id==id)
                if (index != -1) {
                    return;
                } else {
                    collectLists.push(this.data.goods);
                }
                wx.setStorageSync('collectLists', collectLists);
            }else{
                wx.setStorageSync('collectLists', [collectLists])
            }
            wx.showToast({
                title: "收藏成功",
                duration:2000
              })
        }
    },
    //判断该商品是否已收藏
    judgeCollect(){
let collectLists=wx.getStorageSync('collectLists');//获取收藏列表
let id = this.data.goods.id;
if(collectLists){
   let index=collectLists.findIndex(item=>item.id==id)
   if(index==-1){
       this.setData({
           collect:false
       })
   }else{
    this.setData({
        collect:true
    })
   }
}
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let id = options.id;
        getGoodsDetail({
            "goods_id": id
        }).then(data => {
            let {
                goods_id,
                goods_name,
                goods_small_logo,
                pics,
                goods_price,
                goods_introduce
            } = data.message;
            let goods = {
                id: goods_id,
                goodsImage: goods_small_logo,
                goodsName: goods_name,
                goodsImgs: pics,
                goodsPrice: goods_price,
                goodsPriceOld: goods_price + 100,
                goodsDetails: goods_introduce
            }
            this.setData({
                goods
            })
            this.judgeCollect();
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            num: wx.getStorageSync('goodsCarList').length
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})